//go:build linux || windows

// Add By : 陈狍子 e4ting@qq.com 2024-08-14 16:19:32

package main

import (
	"fmt"
	"time"

	// "fmt"
	// "os"
	// // "io"
	// "bytes"
	// "encoding/hex"
	// "encoding/json"
	// "io/ioutil"
	// "log"
	// "net/http"
	// "github.com/google/uuid"

	"wick/internal/model"
	"wick/pkg/util"
)

type Editor struct {
	Filename string
	DestUuid string
	Uuid     string
	Target   string
	Hash     uint
}

func init() {

}

func (self *Editor) SetUuid(uuid string) {
	self.Uuid = uuid
}

func (self *Editor) SetTarget(rq *model.CopyFileRs) {
	if len(self.Target) <= 0 {
		self.Target = rq.Data.Target
	}
	self.Hash = uint(rq.Data.Hash)
	logger.Println("文件hash变更", self.Target, self.Hash)
}

func (self *Editor) Run() {
	if err := self.CheckPlugin(); err != nil {
		logger.Println("检查本地插件失败", err)
		return
	}
	if err := self.FindEditor(); err != nil {
		logger.Println("查询编辑器失败", err)
		return
	}
	if err := self.PushFile(); err != nil {
		logger.Println("推送文件失败", err)
		return
	}
	if err := self.StartEditor(); err != nil {
		logger.Println("打开编辑器失败", err)
		return
	}
	logger.Println("已打开文件，请前往编辑")
	for {
		time.Sleep(1 * time.Second)
		if err, flag := self.CheckFileChanged(); err != nil {
			logger.Println("发生错误，结束运行: ", err)
			break
		} else if flag {
			if err := self.PullFile(); err != nil {
				logger.Println("下载文件失败: ", err)
				break
			}
			logger.Printf("%s 编辑了文件 %s ，已同步到本地 %s", self.DestUuid, self.Target, self.Filename)
		}
	}
}

// 检查插件是否可用
func (self *Editor) CheckPlugin() error {
	logger.Println("检查插件是否运行")
	if res, err := ConnectServer(); err != nil {
		return err
	} else {
		// logger.Println(res)
		self.SetUuid(res.Data.NodeID)
	}
	logger.Println("插件运行 OK")
	return nil
}

// 查找是否有编辑器
func (self *Editor) FindEditor() error {
	logger.Println("检查是否有编辑器可用")
	self.DestUuid = "E4A2568A01002D"
	logger.Println("编辑器已设置为: ", self.DestUuid)
	return nil
}

// 查找是否有编辑器
func (self *Editor) StartEditor() error {
	logger.Println("检查是否有编辑器可用")
	url := fmt.Sprintf(`%s/api/v2/bot/control`, *API)
	data := model.OpenFileRq{
		Uuid: self.DestUuid,
		File: self.Target,
	}
	res, err := util.Post[model.OpenFileRs](url, data)
	if err != nil {
		logger.Println("启动编辑器请求失败", err)
		return err
	}
	if res.Code != 200 {
		logger.Printf("启动编辑器失败: %+v", res)
		return fmt.Errorf("启动编辑器失败: %+v", res)
	}
	return nil
}

// 上传文件
func (self *Editor) PushFile() error {
	logger.Println("上传文件到编辑器", self.Filename)
	url := fmt.Sprintf(`%s/api/v2/bot/fsctl`, *API)
	data := model.CopyFileRq{
		Uuid:   self.Uuid,
		File:   self.Filename,
		Dest:   self.DestUuid,
		Target: "~",
	}
	res, err := util.Post[model.CopyFileRs](url, data)
	if err != nil {
		logger.Println("copy文件请求失败", err)
		return err
	}
	if res.Code != 200 {
		logger.Printf("copy文件失败: %+v", res)
		return fmt.Errorf("copy文件失败: %+v", res)
	}
	self.SetTarget(res)
	return nil
}

// 检查文件是否修改
func (self *Editor) CheckFileChanged() (error, bool) {
	url := fmt.Sprintf(`%s/api/v2/bot/fscmp`, *API)
	data := model.CmpFileRq{
		Uuid:   self.DestUuid,
		Target: self.Target,
		Hash:   self.Hash,
	}
	res, err := util.Post[model.CmpFileRs](url, data)
	if err != nil {
		logger.Println("compare文件请求失败", err)
		return err, false
	}
	if res.Code != 200 {
		logger.Printf("compare文件失败: %+v", res)
		return fmt.Errorf("compare文件失败: %v", res.Msg), false
	}
	return nil, res.Data
}

// 下载文件
func (self *Editor) PullFile() error {
	// logger.Println("上传文件到编辑器")
	url := fmt.Sprintf(`%s/api/v2/bot/fsctl`, *API)
	data := model.CopyFileRq{
		Uuid:   self.DestUuid,
		File:   self.Target,
		Dest:   self.Uuid,
		Target: self.Filename,
	}
	// return nil
	res, err := util.Post[model.CopyFileRs](url, data)
	if err != nil {
		logger.Println("同步文件请求失败", err)
		return err
	}
	if res.Code != 200 {
		logger.Printf("同步文件失败: %v", res.Msg)
		return fmt.Errorf("同步文件失败: %v", res.Msg)
	}
	logger.Printf("文件已下载到本地 %+v", res)
	self.SetTarget(res)
	return nil
}
